package encaps.utils.rendering;

import encaps.action.Action;
import encaps.resources.Config;
import encaps.resources.Response;
import encaps.utils.StringUtil;
import static encaps.utils.html.HTML.*;
import encaps.utils.html.HTMLElement;
import encaps.utils.html.Styles;
import org.jdom.Element;

public class LinkFactory implements FieldFactory {

    @Override
    public HTMLElement renderField(Element e, boolean edit) {
        HTMLElement link = new HTMLElement(A);
        StringBuilder href = new StringBuilder();
        href.append(Config.getHomePage());
        href.append("?");
        href.append(Action.ACTION);
        href.append("=");
        String actionName = e.getChildTextTrim(Response.KEY);
        href.append(actionName);

        String entityId = e.getChildTextTrim(Response.VALUE);
        if (!StringUtil.isEmpty(entityId)) {
            href.append("&");
            href.append(Action.ID);
            href.append("=");
            href.append(entityId);
        }
        link.addAttribute(HREF, href.toString());

        String label = e.getChildTextTrim(Response.LABEL);
        link.addContent(label);

        HTMLElement p = new HTMLElement(P);
        p.addAttribute(CLASS, Styles.PLINK);
        p.addContent(link);
        return p;
    }
}
